package com.xn.myyxcampus.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.util.LogUtils;
import com.xn.myyxcampus.MyApplication;
import com.xn.myyxcampus.R;
import com.xn.myyxcampus.bean.VoiceList;
import com.xn.myyxcampus.bean.VoiceListDetail;
import com.xn.myyxcampus.http.URLs;
import com.xn.myyxcampus.utils.TimeUtil;

public class VoiceListAdatper extends BaseAdapter {

	private VoiceList voiceList = null;
	private Context context = MyApplication.getContext();
	private String bitmapCache = context.getExternalCacheDir()
			+ "/tracy/Voice/";
	private BitmapUtils bitmapUtil = new BitmapUtils(context, bitmapCache);

	public void loadData(VoiceList list) {
		this.voiceList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null == voiceList) {
			return 0;
		} else {
			return voiceList.getList().size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub

		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (null == voiceList) {
			return null;
		} else {
			ViewHolder holder;
			if (null == convertView) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.voicelist_item, null);
				LogUtils.i("relativeLayout"+convertView);
				holder.voiceHead = (ImageView) convertView
						.findViewById(R.id.iv_voiceHead);
				holder.voiceName = (TextView) convertView
						.findViewById(R.id.tv_voiceName);
				holder.voiceDefaultPhoto = (ImageView) convertView
						.findViewById(R.id.iv_voiceDefaultPhoto);
				holder.voiceTime = (TextView) convertView
						.findViewById(R.id.tv_voiceTime);
				holder.voicePhoneKind = (TextView) convertView
						.findViewById(R.id.tv_voicePhoneKind);
				holder.voiceItemMenu = (ImageView) convertView
						.findViewById(R.id.iv_voiceItemMenu);
				holder.voiceContent = (TextView) convertView
						.findViewById(R.id.tv_voiceContent);
				holder.voiceContentImageLayout = (LinearLayout) convertView
						.findViewById(R.id.voice_content_image_layout);
				holder.voiceContentImage = (ImageView) convertView
						.findViewById(R.id.iv_voiceContentImage);
				holder.sendComment = (ImageView) convertView
						.findViewById(R.id.iv_sendComment);
				holder.voiceCommentCount = (TextView) convertView
						.findViewById(R.id.tv_voiceCommentCount);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			VoiceListDetail detail = voiceList.getList().get(position);
			holder.voiceName.setText(detail.getName());
			holder.voiceTime.setText(TimeUtil.getCurrrentTime(Long
					.parseLong(detail.getTime())));
			holder.voicePhoneKind.setText(detail.getFromPhone());
			holder.voiceContent.setText(detail.getContent());
			bitmapUtil.configDefaultLoadFailedImage(R.drawable.empty_photo);
			bitmapUtil.configDefaultLoadingImage(R.drawable.empty_photo);
			final String userImgUrl = URLs.VOICE_HEAD_PHOTO
					+ detail.getUpic().replace('+', '/');
			bitmapUtil.display(holder.voiceHead, userImgUrl);
			if ("nopic".equals(detail.getPic())) {
				holder.voiceContentImageLayout.setVisibility(View.GONE);
			} else {
				final String contentImgUrl = URLs.VOICE_PHOTO
						+ detail.getPic().replace('+', '/');

				bitmapUtil.display(holder.voiceContentImage, contentImgUrl);
			}
			holder.voiceCommentCount.setText(detail.getCommentNum());

			holder.sendComment.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

				}
			});
			return convertView;
		}
	}

	class ViewHolder {
		public ImageView voiceHead;
		public TextView voiceName;
		public ImageView voiceDefaultPhoto;
		public TextView voiceTime;
		public TextView voicePhoneKind;
		public ImageView voiceItemMenu;
		public TextView voiceContent;
		public LinearLayout voiceContentImageLayout;
		public ImageView voiceContentImage;
		public ImageView sendComment;
		public TextView voiceCommentCount;
	}

}
